package com.springdatecrm.web.controller;

import com.springdatecrm.web.entity.Role;
import com.springdatecrm.web.service.RoleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class RoleController {

    @Resource
    private RoleService roleService;

    @RequestMapping(value = "/role/json")
    @ResponseBody
   public List<Role> findAllRoles() {
        List<Role> list = roleService.findAllRoles();
        return list;
    }

    @RequestMapping(value = "/role/list")
    public String roleList(String roleName, Model model) {
        List<Role> list = roleService.findAllRolesInfo(roleName);
        model.addAttribute("roleName",roleName);
        model.addAttribute("roles",list);
        return "role/list";
    }


    @RequestMapping(value = "/role/add")
    public String roleAdd(Model model) {
        List<Role> list = roleService.findAllRoles();
        model.addAttribute("roles",list);
        return "role/add";
    }

    @RequestMapping(value = "/role/save",method = RequestMethod.POST)
    public String roleAddSave(Role role) {
        roleService.insert(role);
        return "redirect:/role/list";
    }


}
